package com.web.dao;

import com.web.entity.UserAccount;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.math.BigDecimal;

/**
 * @author FDjavaone
 */
public interface UserAccountMapper {
    @Delete({
        "delete from t_user_account",
        " where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
        "insert into t_user_account (u_id, profit, ",
        " create_time, modify_time)",
        " values (#{uId,jdbcType=BIGINT}, #{profit,jdbcType=DECIMAL}, ",
        " #{createTime,jdbcType=TIMESTAMP}, #{modifyTime,jdbcType=TIMESTAMP})"
    })
    @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Long.class)
    int insert(UserAccount record);

    @InsertProvider(type=UserAccountSqlProvider.class, method="insertSelective")
    @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Long.class)
    int insertSelective(UserAccount record);

    @Select({
        "select",
        " id, u_id, profit, create_time, modify_time",
        " from t_user_account",
        " where id = #{id,jdbcType=BIGINT}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
        @Result(column="u_id", property="uId", jdbcType=JdbcType.BIGINT),
        @Result(column="profit", property="profit", jdbcType=JdbcType.DECIMAL),
        @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="modify_time", property="modifyTime", jdbcType=JdbcType.TIMESTAMP)
    })
    UserAccount selectByPrimaryKey(Long id);

    @UpdateProvider(type=UserAccountSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(UserAccount record);

    @Update({
        "update t_user_account",
        " set u_id = #{uId,jdbcType=BIGINT},",
          " profit = #{profit,jdbcType=DECIMAL},",
          " create_time = #{createTime,jdbcType=TIMESTAMP},",
          " modify_time = #{modifyTime,jdbcType=TIMESTAMP}",
        " where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(UserAccount record);


    @Update({
            "update t_user_account",
            " set ",
            " profit =profit+#{totalPrice,jdbcType=DECIMAL}",
            " where u_id = #{id,jdbcType=BIGINT}"
    })
    void profitAdd(@Param("totalPrice") BigDecimal totalPrice, @Param("id") Long id);

    @Select({
            "select",
            " id, u_id, profit, create_time, modify_time",
            " from t_user_account",
            " where u_id = #{uid,jdbcType=BIGINT}"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
            @Result(column="u_id", property="uId", jdbcType=JdbcType.BIGINT),
            @Result(column="profit", property="profit", jdbcType=JdbcType.DECIMAL),
            @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="modify_time", property="modifyTime", jdbcType=JdbcType.TIMESTAMP)
    })
    UserAccount selectByUid(@Param("uid") Long uid);

    @Update({
            "update t_user_account",
            " set ",
            " profit =profit-#{totalPrice,jdbcType=DECIMAL}",
            " where u_id = #{id,jdbcType=BIGINT}"
    })
    void profitSubtract(@Param("totalPrice") BigDecimal totalPrice, @Param("id") Long id);
}